<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回路径中的目录部分</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.delete.html">« delete</a></li>
      <li style="float: right;"><a href="function.disk-free-space.html">disk_free_space »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.filesystem.html">文件系统函数</a></li>
    <li>返回路径中的目录部分</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.dirname" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">dirname</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">dirname</span> &mdash; <span class="dc-title">返回路径中的目录部分</span></p>

 </div>
 
  <div class="refsect1 description" id="refsect1-function.dirname-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>dirname</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$path</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$levels</code><span class="initializer"> = 1</span></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   给出一个包含有指向一个文件的全路径的字符串，本函数返回去掉文件名后的目录名，且目录深度为 <code class="parameter">levels</code> 级。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    <span class="function"><strong>dirname()</strong></span> 纯粹基于输入字符串操作，
    它不会受实际文件系统和类似 &quot;<code class="literal">..</code>&quot; 的路径格式影响。
   </p>
  </p></blockquote>
  <div class="caution"><strong class="caution">警告</strong>
   <p class="para">
    在 Windows 上，<span class="function"><strong>dirname()</strong></span>
    假设当前设置的代码页，因此要查看具有多字节字符路径的正确目录名称，必须设置匹配的代码页。如果
    <code class="parameter">path</code> 包含当前代码页无效的字符，则 <span class="function"><strong>dirname()</strong></span>
    的行为是未定义的。
   </p>
   <p class="para">
    在其它系统上，<span class="function"><strong>dirname()</strong></span> 假设 <code class="parameter">path</code>
    是以 ASCII 兼容编码进行编码的。否则函数的行为是未定义的。
   </p>
  </div>
 </div>

 
  <div class="refsect1 parameters" id="refsect1-function.dirname-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">path</code></dt>

     <dd>

      <p class="para">
       一个路径。
      </p>
      <p class="para">
       在 Windows 中，斜线（<code class="literal">/</code>）和反斜线（<code class="literal">\</code>）都可以用作目录分隔符。在其它环境下是斜线（<code class="literal">/</code>）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">levels</code></dt>

     <dd>

      <p class="para">
       要向上的父目录数量。
      </p>
      <p class="para">
       整型，必须大于 0。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.dirname-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回 path 的父目录。如果在 <code class="parameter">path</code>
   中没有斜线，则返回一个点（&#039;<code class="literal">.</code>&#039;），表示当前目录。否则返回的是把
   <code class="parameter">path</code> 中结尾的
   <code class="literal">/component</code>（最后一个斜线以及后面部分）去掉之后的字符串。
  </p>

  <div class="caution"><strong class="caution">警告</strong>
   <p class="para">
    Be careful when using this function in a loop that can reach the
    top-level directory as this can result in an infinite loop.
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">'.'</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Will&nbsp;return&nbsp;'.'.<br /></span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">'/'</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Will&nbsp;return&nbsp;`\`&nbsp;on&nbsp;Windows&nbsp;and&nbsp;'/'&nbsp;on&nbsp;*nix&nbsp;systems.<br /></span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">'\\'</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Will&nbsp;return&nbsp;`\`&nbsp;on&nbsp;Windows&nbsp;and&nbsp;'.'&nbsp;on&nbsp;*nix&nbsp;systems.<br /></span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">'C:\\'</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;Will&nbsp;return&nbsp;'C:\'&nbsp;on&nbsp;Windows&nbsp;and&nbsp;'.'&nbsp;on&nbsp;*nix&nbsp;systems.<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
  </div>
 </div>

 
 <div class="refsect1 changelog" id="refsect1-function.dirname-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.0.0</td>
       <td>
        添加可选的 <code class="parameter">levels</code> 参数。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


  <div class="refsect1 examples" id="refsect1-function.dirname-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2143">
    <p><strong>示例 #1 <span class="function"><strong>dirname()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">"/etc/passwd"</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">"/etc/"</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">"."</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">"C:\\"</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #DD0000">"/usr/local/lib"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
/etc
/ (or \ on Windows)
.
C:\
/usr
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.dirname-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.basename.html" class="function" rel="rdfs-seeAlso">basename()</a> - 返回路径中的文件名部分</span></li>
    <li class="member"><span class="function"><a href="function.pathinfo.html" class="function" rel="rdfs-seeAlso">pathinfo()</a> - 返回文件路径的信息</span></li>
    <li class="member"><span class="function"><a href="function.realpath.html" class="function" rel="rdfs-seeAlso">realpath()</a> - 返回规范化的绝对路径名</span></li>
   </ul>
  </p>
 </div>

 
</div></div></div></body></html>